** This file produces Figure 4.1 (comparison of legislator ideology and 
** district ideology)

* Open dataset of campaign finance laws
use masterdataset, clear

* Keep the 2006 election cycle, sort, and then save for later merging
keep if year==2006
sort statabbr
save temp, replace

* Open the dataset of Shor/McCarty ideal points
use npat1996_2008, clear

* Keep the 2008 election cycle and keep only state senators
keep if year==2008
drop if sdistrict==""

* Rename variables and sort for merging with Catalist ideology
ren statabbr state
sort state st_id
merge state st_id using catalist_ideology
drop if _m==2
drop _m

* Put ideology on 0 to 100 scale
replace ideology=(ideology*5)-2.5

* Use regression to convert Shor/McCarty scores to Catalist 100 point scale
reg ideology np_score
predict legislator_ideology
replace legislator_ideology=0 if legislator_ideology<0
replace legislator_ideology=100 if legislator_ideology>100 & legislator_ideology~=.

* Reverse scale so lower values are liberal and higher values conservative
replace legislator_ideology=(legislator_ideology-100)*-1

* Rename the state variable for merging and drop the ideology variable
ren state statabbr
drop ideology

* Merge in a version of the dataset that reconciles district names for MA
sort statabbr sdistrict
merge statabbr sdistrict using slegs_ma_reconcile
drop if _m==2
drop _m

* The following codes fix the district numbers to prepare for merging with 
gen district=real(sdistrict)
sort statabbr sdistrict
replace district=1 if statabbr=="AK" & sdistrict=="A"
replace district=district[_n-1]+1 if statabbr=="AK" & district==.
replace district=code if statabbr=="MA"

* Merge dataset with a dataset of the ideological point estimates of citizens
* in state senate districts 
sort statabbr district
merge statabbr district using constituent_ideology_senate
tab _m
drop if _m<3
drop _m

* Rename variable for the average ideology in a state senate district
ren ideology_mean ideology

* Reverse direction of variable so liberal is on left and conservative on right
replace ideology=(ideology-100)*-1

* Drop any state senate districts where the sample of citizens was less than 200
drop if sample<200

* Create Figure 4.1
twoway kdensity legislator_ideology if party=="D", lc(black) lp(shortdash) || kdensity legislator_ideology if party=="R", lc(black) lp(longdash) || kdensity ideology, lc(black) scheme(s1color) xtitle("Ideology") legend(lab(1 "Dem. Legislators") lab(2 "Rep. Legislators") lab(3 "District Mean") col(3)) xlabel(0 25 50 75 100)
graph export fig_4_1.eps, replace



